package mysql

import (
	"gorm.io/gorm"
	"me/123/common/global"
	"me/123/common/model"
)

func QueryUserInfo(mobile string) (result *model.Users, err error) {
	err = global.DB.Where("mobile = ?", mobile).Limit(1).Find(&result).Error
	return
}

func QueryCoupons(id int64) (result *model.Coupons, err error) {
	err = global.DB.Model(&model.Coupons{}).Where("id = ?", id).Limit(1).Find(&result).Error
	return
}

func QueryUserCouponsInfo(uid int64, id int64) (result *model.UserCoupons, err error) {
	err = global.DB.Model(&model.UserCoupons{}).Where("uid = ?", uid).Where("coupons_id = ?", id).Limit(1).Find(&result).Error
	return
}

func AddUserCoupons(m *model.UserCoupons) error {
	return global.DB.Create(&m).Error
}

func QueryUserInfoByUid(uid int64) (result *model.Users, err error) {
	err = global.DB.Where("id = ?", uid).Limit(1).Find(&result).Error
	return
}

func UserIntegral(uid int64, integral float64, tx *gorm.DB) error {
	return tx.Model(&model.Users{}).Where("id = ?", uid).Update("integral", integral).Error
}

func DestroyedCoupons(uid int64, id int64, sta int) error {
	return global.DB.Model(&model.UserCoupons{}).Where("uid = ? and coupons_id = ?", uid, id).Update("coupons_status", sta).Error
}
